package com.qiyue.common.context;

import com.qiyue.common.constant.CommonConstants;
import com.qiyue.utils.string.StringUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * Created by y747718944 on 2018/2/8
 *  利用线程获取context id
 */
public class BaseContextHandler {

    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key){
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }


    public static void setToken(String token){set(CommonConstants.ContextConstant.CONTEXT_KEY_USER_TOKEN,token);}

    public static String getToken(){
        Object value = get(CommonConstants.ContextConstant.CONTEXT_KEY_USER_TOKEN);
        return StringUtil.getObjectValue(value);
    }


    public static void setName(String name){set(CommonConstants.ContextConstant.CONTEXT_KEY_USER_NAME,name);}

    public static String getName(){
        Object value = get(CommonConstants.ContextConstant.CONTEXT_KEY_USER_NAME);
        return StringUtil.getObjectValue(value);
    }

    public static void setUserID(String userID){
        set(CommonConstants.ContextConstant.CONTEXT_KEY_USER_ID,userID);
    }

    /**
     * 从本地线程中获取 当前用户id
     * @return
     */
    public static String getUserID(){
        Object value = get( CommonConstants.ContextConstant.CONTEXT_KEY_USER_ID);
        return returnObjectValue(value);
    }

    public static void setUserAccount(String account){
        set(CommonConstants.ContextConstant.CONTEXT_KEY_USER_ACCOUNT,account);
    }

    /**
     * 从本地线程中获取 当前用户账号
     * @return
     */
    public static String getUserAccount(){
        Object value = get(CommonConstants.ContextConstant.CONTEXT_KEY_USER_ACCOUNT);
        return returnObjectValue(value);
    }

    public static void setUserHost(String host){
        set(CommonConstants.ContextConstant.CONTEXT_KEY_USER_HOST, host);
    }

    public static String getUserHost(){
        Object value =  get(CommonConstants.ContextConstant.CONTEXT_KEY_USER_HOST);
        return returnObjectValue(value);
    }

    private static String returnObjectValue(Object value) {
        return value==null?null:value.toString();
    }

    public static void remove(){
        threadLocal.remove();
    }

//    @RunWith(MockitoJUnitRunner.class)
//    public static class UnitTest {
//        private Logger logger = LoggerFactory.getLogger(UnitTest.class);
//
//        @Test
//        public void testSetContextVariable() throws InterruptedException {
//            BaseContextHandler.set("test", "main");
//            new Thread(()->{
//                BaseContextHandler.set("test", "moo");
//
//                try {
//                    Thread.sleep(3000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                assertEquals(BaseContextHandler.get("test"), "moo");
//                logger.info("thread one done!");
//            }).start();
//            new Thread(()->{
//                BaseContextHandler.set("test", "moo2");
//                assertEquals(BaseContextHandler.get("test"), "moo2");
//                logger.info("thread two done!");
//            }).start();
//
//            Thread.sleep(5000);
//            assertEquals(BaseContextHandler.get("test"), "main");
//            logger.info("main one done!");
//        }
//
//        @Test
//        public void testSetUserInfo(){
//            BaseContextHandler.setUserID("test");
//            assertEquals(BaseContextHandler.getUserID(), "test");
//            BaseContextHandler.setUserAccount("test2");
//            assertEquals(BaseContextHandler.getUserAccount(), "test2");
//        }
//    }
}
